def v(t):
    return t**2  # 定义速度函数

def displacement(a, b, n):
    """
    计算变速直线运动的位移
    """
    dt = (b - a) / n  # 时间间隔
    total_displacement = 0.0
    
    for i in range(n):
        ti = a + i * dt  # 取时刻 ξ_i
        total_displacement += v(ti) * dt  # 求和 v(ξ_i) Δt_i
    
    return total_displacement

# 计算位移
a, b = 0, 1
n = 1000
approx_displacement = displacement(a, b, n)

print(f"位移近似值 (n={n}): {approx_displacement:.6f}")
print(f"精确值: {1/3:.6f}")
print(f"误差: {abs(approx_displacement - 1/3):.6f}")